from PyQt6.QtWidgets import QApplication, QSplitter, QWidget, QVBoxLayout, QGridLayout
from PyQt6.QtCore import Qt, pyqtSignal, pyqtSlot, QThread
from qfluentwidgets import SmoothScrollArea
import time
from .components.oddsCard import OddsCard
from .components.calculator import CalculatorCard

from main_crawler import run_crawler
from data.processedData import ProcessedData

# 爬虫线程类

class TopWidget(QWidget):
    """顶部页面控件"""
    
    # 定义一个信号，传递类型为 自定义对象
    signal_topWidget = pyqtSignal(object)
    
    def __init__(self, queue, stop_event, parent=None):
        super().__init__(parent)
        self.queue = queue
        self.stop_event = stop_event
        self.top_layout = QVBoxLayout(self)
        # self.load_data()
        
        self.top_layout.setContentsMargins(0, 0, 0, 0)
        self.top_layout.setSpacing(2)  # 设置卡片内部控件的间距
        self.top_layout.setAlignment(Qt.AlignmentFlag.AlignTop)

        # 启动定时器，每秒检查一次队列
        self.timer = self.startTimer(1000)

    def timerEvent(self, event):
        # 检查队列中是否有新数据
        if not self.queue.empty():
            games = self.queue.get()
            # 清空现有布局中的所有卡片
            while self.top_layout.count():
                child = self.top_layout.takeAt(0)
                if child.widget():
                    child.widget().deleteLater()
            # 根据数据列表创建新的卡片
            for item in games:
                card = OddsCard(item, self)
                self.top_layout.addWidget(card)
                # 将子组件的信号连接到父组件的槽函数
                card.signal_odds.connect(self.receive_oddsCard_signal)

    def closeEvent(self, event):
        # 设置停止事件标志，通知爬虫进程停止
        self.stop_event.set()
        event.accept()

    @pyqtSlot(object)
    def receive_oddsCard_signal(self, item):
        """接受从oddsCard发出的信号, 并将数据传递给football做中转"""
        # 处理子组件发出的信号
        self.signal_topWidget.emit(item)    # 发出信号，并携带数据




class BottomWidget(QWidget):
    """底部页面控件"""
    
    # 定义一个信号，传递类型为 自定义对象
    signal_bottomWidget = pyqtSignal(object)
    
    def __init__(self, parent=None):
        super().__init__(parent)
        self.bottom_layout = QVBoxLayout(self)
        calculator = CalculatorCard()
        self.bottom_layout.addWidget(calculator)
        self.bottom_layout.setContentsMargins(0, 0, 0, 0)
        self.bottom_layout.setSpacing(0)  # 设置卡片内部控件的间距
        # self.bottom_layout.setAlignment(Qt.AlignmentFlag.AlignTop)
        
        # 将信号连接到槽函数, 将底部页面(BottomWidget)的信号与计算器(calculator)类进行连接
        self.signal_bottomWidget.connect(calculator.receive_bottom_signal)
        # self.setStyleSheet("background-color: lightgreen; border: 1px solid #ddd; border-radius: 8px; padding: 12px;")

    @pyqtSlot(object)
    def receive_football_signal(self, item):
        """接受从football中转发来的信号, 并将数据继续发送给卡片calculator类"""
        # 处理子组件发出的信号
        self.signal_bottomWidget.emit(item)    # 发出信号，并携带数据


class Football(QWidget):
    """足球页面控件"""
    
    def __init__(self, queue, stop_event, parent=None):
        super().__init__(parent)
        self.queue = queue
        self.stop_event = stop_event
        self.gridLayout = QGridLayout(self)
        self.setup_ui()
        
    def setup_ui(self):
        # 创建 QSplitter
        self.splitter = QSplitter(Qt.Orientation.Vertical, self)

        # 创建顶部和底部页面控件
        self.top_widget = TopWidget(self.queue, self.stop_event)
        self.bottom_widget = BottomWidget()
        
        # 将信号连接到槽函数, 用于顶部页面(TopWidget) - 底部页面(BottomWidget)进行连接, 用于中转
        self.top_widget.signal_topWidget.connect(self.bottom_widget.receive_football_signal)
        
        # 设置子控件的样式
        self.top_widget.setStyleSheet("background-color: lightblue; border: 1px solid #ddd; border-radius: 8px; padding: 12px;")
        self.bottom_widget.setStyleSheet("background-color: lightgreen; border: 1px solid #ddd; border-radius: 8px; padding: 12px;")
        
        # 创建平滑滚动区域
        self.scroll_area = SmoothScrollArea(self)
        # 创建内容区域
        self.scroll_view = QWidget()
        self.scroll_layout = QVBoxLayout(self.scroll_view)
        self.scroll_layout.addWidget(self.top_widget)
        # 将内容区域设置到滚动区域
        self.scroll_area.setWidget(self.scroll_view)
        self.scroll_area.setWidgetResizable(True)

        # 设置滚动区域的透明背景
        self.scroll_area.setStyleSheet("QScrollArea{background: transparent; border: none}")
        self.scroll_view.setStyleSheet("QWidget{background: transparent}")
        
        # 将页面控件添加到 QSplitter 中
        self.splitter.addWidget(self.scroll_area)
        self.splitter.addWidget(self.bottom_widget)
        self.scroll_layout.setContentsMargins(0, 0, 0, 0)

        # 设置主布局
        self.gridLayout.addWidget(self.splitter)
        # 设置布局的边距和间距为 0
        self.gridLayout.setContentsMargins(0, 0, 0, 0)

